package br.com.searchpeople.database;

import br.com.searchpeople.model.UserModel;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SingletonAccountBD {
	
	private SearchPeopleBDOpenHelper helper;
	
	private static SingletonAccountBD instance;
	
	private UserModel user;
	
	private SingletonAccountBD(){
		user = new UserModel();
	}
	
	public static SingletonAccountBD getInstance(){
		if (instance == null) {
			instance = new SingletonAccountBD();
		} 
		
		return instance;
	}
	
	public UserModel getActualAccount(Context context, String login) {
		helper = new SearchPeopleBDOpenHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		String selectQuery = "SELECT * FROM accounts WHERE email = '" + login +"'";
		Cursor cursor = db.rawQuery(selectQuery, null);
		while(cursor.moveToNext()) {
			user.setId(cursor.getInt(0));
			user.setUser_login(cursor.getString(1));
			user.setUser_password(cursor.getString(2));
			user.setUser_confirm_password(cursor.getString(3));
			user.setUser_nickname(cursor.getString(4));
		}
		db.close();
		return user;
	}
	
	public UserModel getAccountByID(Context context, int id) {
		helper = new SearchPeopleBDOpenHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		String selectQuery = "SELECT * FROM accounts WHERE account_id = '" + id +"'";
		Cursor cursor = db.rawQuery(selectQuery, null);
		while(cursor.moveToNext()) {
			user.setId(cursor.getInt(0));
			user.setUser_login(cursor.getString(1));
			user.setUser_password(cursor.getString(2));
			user.setUser_confirm_password(cursor.getString(3));
			user.setUser_nickname(cursor.getString(4));
		}
		db.close();
		return user;
	}
	
	public String getAccountNickname(Context context, String login) {
		String result;
		helper = new SearchPeopleBDOpenHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		String selectQuery = "SELECT nickname FROM accounts WHERE email = '" + login +"'";
		Cursor cursor = db.rawQuery(selectQuery, null);
		if (!cursor.moveToNext()) {
			result = "";
			db.close();
		} else {
			result = cursor.getString(0);
			db.close();
		}
		
		return result;
	}
	
	public boolean isAccount(Context context, String login, String password) {
		helper = new SearchPeopleBDOpenHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		String selectQuery = "SELECT * FROM accounts WHERE email = '" + login + "' AND password = '" + password + "';";
		Cursor cursor = db.rawQuery(selectQuery, null);

		if (!cursor.moveToNext()) {

			db.close();
			return false;
		
		} else {

			db.close();
			return true;
		}
	}
	
	public boolean userExists(Context context, String login) {
		helper = new SearchPeopleBDOpenHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		String selectQuery = "SELECT * FROM accounts WHERE email = '" + login +  "';";
		Cursor cursor = db.rawQuery(selectQuery, null);
		if (!cursor.moveToNext()) {

			db.close();
			return false;
		} else {
			db.close();
			return true;
		}
	}
	
	public void addAccount(Context context, UserModel account) {

		helper = new SearchPeopleBDOpenHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		try {
			values.put("email", account.getUser_login());
			values.put("password", account.getUser_password());
			values.put("confirm_password", account.getUser_confirm_password());
			values.put("nickname", account.getUser_nickname());
			values.put("authtoken", account.getAuthtoken());
			db.insert("accounts", null, values);
		} catch (Exception e) {
			Log.e("SearchPeople db", "Um erro ocorreu durante a inserção da tabela: " + e.toString(), e);
		} finally { 
			db.close();
		}
	}
	
	

}
